package com.sxfq.loansupermarket.backsystem.modular.report.service.impl;

import com.sxfq.loansupermarket.backsystem.common.persistence.model.Dept;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SearchWholeHospitalInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.WholeHospitalInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IGetTotleDoctorService;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IGetTotleService;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IWholeHospitalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class WholeHospitalsServiceImpl implements IWholeHospitalsService {
    @Autowired
    private IGetTotleService getTotleService;

    @Autowired
    private IGetTotleDoctorService getTotleDoctorService;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<WholeHospitalInfo> getList(SearchWholeHospitalInfo searchInfo) {
        String sql = getTotleService.getTotleSqlDepartment(searchInfo);
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getOneChannelList(SearchWholeHospitalInfo searchInfo) {
        String getabbreviationsql = "select id,abbreviation,simplename from dept t where t.simplename='" + searchInfo.getArrivalStore() + "'";
        Dept dept = sqlMapper.selectOne(getabbreviationsql, Dept.class);
        String tableHead = "";
        if (dept == null) {
            tableHead = ShiroKit.getUser().getAbbreviation();
        } else {
            tableHead = dept.getAbbreviation();
        }
        String tableName = tableHead + "customer_info";
        String sqla = " ";
        if (!"ks".equals(searchInfo.getDimensionSelectionCustomerAll()) && StringUtil.isNotEmpty(searchInfo.getDimensionSelectionCustomerAll())) {
            sqla = " and a.treatment_unit='" + searchInfo.getDimensionSelectionCustomerAll() + "'";
        }
        String sql1 = " select a.id,a.one_channel from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' " +
                "and a.arrival_time<='" + searchInfo.getEndTime() + "' " + sqla + " group by one_channel order by a.one_channel";
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = " ";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getOneChannel();
                String condition = "";
                if (classification == "" || classification == null) {
                    condition = "b.one_channel is null";
                } else {
                    condition = "b.one_channel='" + classification + "'";
                }
                sql += getTotleService.getsqlDepartment(searchInfo, classification, condition,condition, condition, condition, i, list1);
            }
            sql += " union all ";
            sql += getTotleService.getTotleSqlDepartment(searchInfo);
        } else {
            sql = getTotleService.getTotleSqlDepartment(searchInfo);
        }
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getOneMediumList(SearchWholeHospitalInfo searchInfo) {
        String getabbreviationsql = "select id,abbreviation,simplename from dept t where t.simplename='" + searchInfo.getArrivalStore() + "'";
        Dept dept = sqlMapper.selectOne(getabbreviationsql, Dept.class);
        String tableHead = "";
        if (dept == null) {
            tableHead = ShiroKit.getUser().getAbbreviation();
        } else {
            tableHead = dept.getAbbreviation();
        }
        String tableName = tableHead + "customer_info";
        String sqla = " ";
        if (!"ks".equals(searchInfo.getDimensionSelectionCustomerAll()) && StringUtil.isNotEmpty(searchInfo.getDimensionSelectionCustomerAll())) {
            sqla = " and a.treatment_unit='" + searchInfo.getDimensionSelectionCustomerAll() + "'";
        }
        String sql1 = " select a.id,a.one_medium from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' " +
                "and a.arrival_time<='" + searchInfo.getEndTime() + "' " + sqla + " group by one_medium order by a.one_medium";
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = " ";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getOneMedium();
                String condition = "";
                if (classification == "" || classification == null) {
                    condition = "b.one_medium is null";
                } else {
                    condition = "b.one_medium='" + classification + "'";
                }
                sql += getTotleService.getsqlDepartment(searchInfo, classification, condition,condition, condition, condition, i, list1);
            }
            sql += " union all ";
            sql += getTotleService.getTotleSqlDepartment(searchInfo);
        } else {
            sql = getTotleService.getTotleSqlDepartment(searchInfo);
        }
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getCustomerAgeList(SearchWholeHospitalInfo searchInfo) {
        String getabbreviationsql = "select id,abbreviation,simplename from dept t where t.simplename='" + searchInfo.getArrivalStore() + "'";
        Dept dept = sqlMapper.selectOne(getabbreviationsql, Dept.class);
        String tableHead = "";
        if (dept == null) {
            tableHead = ShiroKit.getUser().getAbbreviation();
        } else {
            tableHead = dept.getAbbreviation();
        }
        String tableName = tableHead + "customer_info";
        String sqla = " ";
        if (!"ks".equals(searchInfo.getDimensionSelectionCustomerAll()) && StringUtil.isNotEmpty(searchInfo.getDimensionSelectionCustomerAll())) {
            sqla = " and a.treatment_unit='" + searchInfo.getDimensionSelectionCustomerAll() + "'";
        }
        String sql1 = " select a.id,a.age_range from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' " +
                "and a.arrival_time<='" + searchInfo.getEndTime() + "' " + sqla + " group by age_range order by a.age_range";
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = "";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getAgeRange();
                String condition = "";
                if (classification == "" || classification == null) {
                    condition = "b.age_range is null";
                } else {
                    condition = "b.age_range='" + classification + "'";
                }
                sql += getTotleService.getsqlDepartment(searchInfo, classification, condition,condition, condition, condition, i, list1);
            }
            sql += " union all ";
            sql += getTotleService.getTotleSqlDepartment(searchInfo);
        } else {
            sql = getTotleService.getTotleSqlDepartment(searchInfo);
        }

        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getTcNameList(SearchWholeHospitalInfo searchInfo) {
        String getabbreviationsql = "select id,abbreviation,simplename from dept t where t.simplename='" + searchInfo.getArrivalStore() + "'";
        Dept dept = sqlMapper.selectOne(getabbreviationsql, Dept.class);
        String tableHead = "";
        if (dept == null) {
            tableHead = ShiroKit.getUser().getAbbreviation();
        } else {
            tableHead = dept.getAbbreviation();
        }
        String tableName = tableHead + "customer_info";
        String tableName1 = tableHead + "deal_info";
        String tableName2 = tableHead + "recharge_info";
        String sql1 = " ";
        sql1 = " select * from ( select a.id,a.field_tc as tcName from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' " +
                " and a.arrival_time<='" + searchInfo.getEndTime() + "' union all " +
                " select a.id,a.tc_name as tcName from " + tableName1 + " a where a.get_time>='" + searchInfo.getStartTime() + "' " +
                " and a.get_time<='" + searchInfo.getEndTime() + "'" +
                " union all " +
                " select a.id,a.tc_name as tcName from " + tableName2 + " a where a.recharge_time>='" + searchInfo.getStartTime() + "' " +
                " and a.recharge_time<='" + searchInfo.getEndTime() + "'" +
                " and a.tc_name is not null and a.tc_name!='null'" +
                ") t group by t.tcName order by t.tcName";
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = "";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getTcName();
                String condition = "";
                String condition1 = "";
                String conditionOne = "";
                String conditionTwo = "";
                if (classification == "" || classification == null) {
                    condition = "b.field_tc is null";
                    condition1 = "b.field_tc is null";
                } else {
                    condition = "b.field_tc='" + classification + "'";
                    condition1 = "if(c.tc_name is not null,c.tc_name='" + classification + "',b.field_tc='" + classification + "')";
                }
                sql += getTotleService.getsqlDepartment(searchInfo, classification, condition,condition1, conditionOne, conditionTwo, i, list1);
            }
            sql += " union all ";
            sql += getTotleService.getTotleSqlDepartment(searchInfo);
        } else {
            sql = getTotleService.getTotleSqlDepartment(searchInfo);
        }
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getDoctorList(SearchWholeHospitalInfo searchInfo) {
        String getabbreviationsql = "select id,abbreviation,simplename from dept t where t.simplename='" + searchInfo.getArrivalStore() + "'";
        Dept dept = sqlMapper.selectOne(getabbreviationsql, Dept.class);
        String tableHead = "";
        if (dept == null) {
            tableHead = ShiroKit.getUser().getAbbreviation();
        } else {
            tableHead = dept.getAbbreviation();
        }
        String tableName = tableHead + "customer_info";
        String tableName1 = tableHead + "deal_info";
        String sql1 = " ";
        if (!"ks".equals(searchInfo.getDimensionSelectionCustomerAll()) && StringUtil.isNotEmpty(searchInfo.getDimensionSelectionCustomerAll())) {
            sql1 = " select a.id,a.doctor as doctor from  " + tableName1 + " a left join " + tableName + " b on a.customer_id=b.id" +
                    " where a.get_time>='" + searchInfo.getStartTime() + "' " +
                    "and a.get_time<='" + searchInfo.getEndTime() + "'  " +
                    "  group by a.doctor order by a.doctor";
        } else {
            sql1 = " select * from ( select a.id,a.doctor as doctor from " + tableName + " a where a.arrival_time>='" + searchInfo.getStartTime() + "' " +
                    " and a.arrival_time<='" + searchInfo.getEndTime() + "' and a.doctor!='未指定医生' union all " +
                    " select a.id,a.doctor as doctor from " + tableName1 + " a where a.get_time>='" + searchInfo.getStartTime() + "' " +
                    "and a.get_time<='" + searchInfo.getEndTime() + "') t " +
                    "  group by t.doctor order by t.doctor";
        }
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = "";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getDoctor();
                String condition = "";
                if (classification == "" || classification == null) {
                    classification = "";
                    condition = " (a.doctor='' or a.doctor is null) and a.operation_time is not null ";
                } else {
                    condition = " a.doctor='" + classification + "' ";
                }
                sql += getTotleDoctorService.getsqlDepartment(searchInfo, classification, condition, i, list1);
            }
            if ("0".equals(searchInfo.getIsks())) {
                String classifications = "储值";
                String conditions = " (a.doctor is null and a.operation_time is null)  ";
                sql += " union all ";
                sql += getTotleDoctorService.getsqlDepartmentcz(searchInfo, classifications, conditions, list1.size() - 1, list1);
            }
            sql += " union all ";
            sql += getTotleDoctorService.getTotleSqlDepartment(searchInfo);
        } else {
            if ("0".equals(searchInfo.getIsks())) {
                String classifications = "储值";
                String conditions = " (a.doctor is null and a.operation_time is null) ";
                sql += getTotleDoctorService.getsqlDepartmentcz(searchInfo, classifications, conditions, list1.size() - 1, list1);
                sql += " union all ";
                sql += getTotleDoctorService.getTotleSqlDepartment(searchInfo);
            } else {
                sql += getTotleDoctorService.getTotleSqlDepartment(searchInfo);
            }
        }
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }
}